﻿// --------------------------------
// <copyright file="DropDownOptions.cs" company="Faculty of Information Technology, Czech Technical University in Prague">
//     This document is shared by Microsoft Public License (MS-PL)
//     For further details please see http://msdn.microsoft.com/en-us/library/ff648068.aspx or license.txt.
// </copyright>
// <author>Lukáš Molnár</author>

// <email>molnaluk@fit.cvut.cz</email>

// <date>17. 4. 2012 15:41:04</date>

// <summary>
// 
// </summary>
// --------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using Microsoft.Xna.Framework.Media;

namespace FITClub.TextureMenu.Configuration
{
    public class DropDownOptionsConverter : StringConverter
    {
        StandardValuesCollection standardValues;
        StandardValuesCollection svMedia;
        StandardValuesCollection svMusic;
        StandardValuesCollection svManager;
        StandardValuesCollection svDummy;
        StandardValuesCollection svGame;
        private static object Game;

        public DropDownOptionsConverter() :base()
        {
            standardValues = new StandardValuesCollection(new string[] { "Game", "Manager", "MusicPlayer", "MediaPlayer", "this" });
            svDummy = new StandardValuesCollection(new object[] { });
            svMusic = GetPublicMethods(typeof(ExternalClasses.Sounds.MusicPlayer));
            svMedia = GetPublicMethods(typeof(MediaPlayer));
            svManager = GetPublicMethods(typeof(MenuManager));
        }

        private StandardValuesCollection GetPublicMethods(Type tp)
        {
            var coll = (from itm in tp.GetMethods() where itm.IsPublic select  itm.Name ).Distinct().ToArray();
            return new StandardValuesCollection(coll);
        }

        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            MenuItemAction actiom = context.Instance as MenuItemAction;
            if (actiom != null && context.PropertyDescriptor.Name == "Method")
            {
                switch (actiom.Target)
                {
                    case "MusicPlayer":
                    case "MediaPlayer":
                    case "Manager":
                        return true;
                    case "Game":
                        if (svGame == null && Game != null)
                        {
                            svGame = GetPublicMethods(Game.GetType());
                        }
                        return svGame != null;
                    default:
                        return false;
                }
            }
            return context.PropertyDescriptor.Name == "Target";
        }

        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {            
            return true;
        }


        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            MenuItemAction actiom = context.Instance as MenuItemAction;
            
            if (actiom != null && context.PropertyDescriptor.Name == "Method")
            {
                switch (actiom.Target)
                {
                    case "MusicPlayer":
                        return svMusic;
                    case "MediaPlayer":
                        return svMedia;
                    case "Manager":
                        return svManager;
                    case "this":
                        return svDummy;
                    case "Game":
                        if (svGame == null && Game != null)
                        {
                            svGame = GetPublicMethods(Game.GetType());
                        }
                        if (svGame == null)
                        {
                            return svDummy;
                        }
                        return svGame;
                }
            }
            return standardValues;
        }

        public static void InjectData(object game)
        {
            DropDownOptionsConverter.Game = game;
        }

    }
}
